package main

import (
	"fmt"
)

func maxSubArray(nums []int) int {
	if len(nums) < 1 {
		return 0
	}
	d := nums[0]
	result := d
	for i := 1; i < len(nums); i++ {
		if d+nums[i] > nums[i] {
			d = d + nums[i]
		} else {
			d = nums[i]
		}
		if result < d {
			result = d
		}
	}
	return result
}

func main() {
	nums := []int{-2, 1, -3, 4, -1, 2, 1, -5, 4}
	a := maxSubArray(nums)
	fmt.Println(a)
}
